<!DOCTYPE html>
<html>

<head></head>

<body>
    <script type="text/javascript">
        var sock = null;
        var wsuri = "ws://127.0.0.1:12080/echo";
        window.onload = function () {
            console.log("onload");
            sock = new WebSocket(wsuri);
            sock.onopen = function () {
                console.log("connected to " + wsuri);
            }
            sock.onclose = function (e) {
                console.log("connection closed (" + e.code + ")");
            }
            sock.onmessage = function (e) {
                console.log("message received: " + e.data);
            }
        };
        function send() {
            var msg = document.getElementById('message').value;
            if (sock.readyState != 1) {
                alert("WebSocket连接已关闭,请刷新页面重试!");
            }
            sock.send(msg);
        };
    </script>
    <h1>WebSocket Echo Test</h1>
    <form>
        <p>
            Message: <input id="message" type="text" value="Hello, world!">
        </p>
    </form>
    <button onclick="send();">Send Message</button>
</body>

</html>